package yl.hs.bmipfull.controllers.salemanage;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.salemanage.ILinkerInfoService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  联系人控制层<br/>
 * 创建时间: 2021年05月19日 11时16分38秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "联系人Web接口", tags = "销售管理——联系人操作")
@RequestMapping(value = "/api/customer")
@RestController
public class LinkerInfoController extends ControllerBase {

    @Autowired
    ILinkerInfoService service;
    @Autowired
    ISearchViewService svSVC;
    @ApiOperation(value = "查询联系人", notes = "查询联系人")
    @RequestMapping(value = "/select/linker", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询联系人", notes = "查询联系人")
    @RequestMapping(value = {"/selectLinker/{customerSN}","/selectLinker"}, method = RequestMethod.GET)
    public WebResult queryList(@PathVariable(required = false) String customerSN) {
        try {
            return success("查询成功", service.queryListByCustomerSN(customerSN));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询联系人", notes = "分页查询联系人")
    @RequestMapping(value = "/linker/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询联系人", notes = "根据联系人编号查询联系人")
    @RequestMapping(value = "/linker/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存联系人", notes = "查询/更新联系人信息")
    @RequestMapping(value = "/linker", method = RequestMethod.POST)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveLinkerInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else if(result==-1){
                return fail("联系人手机重复，请重新输入");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除联系人信息", notes = "根据联系人编号删除系联系人信息")
    @RequestMapping(path = "/linker/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "联系人编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "根据联系人编号修改联系人状态", notes = "根据联系人编号修改联系人状态")
    @RequestMapping(value = "/linker/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}